package com.zy.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.yygh.common.utils.MD5;
import com.zy.yygh.hosp.service.HospitalSetService;
import com.zy.yygh.model.hosp.HospitalSet;
import com.zy.yygh.common.result.Result;
import com.zy.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin //允许跨域访问
public class HospitalSetController {

//    注入service
    @Autowired
    private HospitalSetService hospitalSetService;


//    1.查询医院设置表中的所有信息
    @ApiOperation(value = "获取所有信息")
    @GetMapping("findAll")
    public Result findAllHostSet(){
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

//    2.逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院信息")
    @DeleteMapping("{id}")
    public Result removeHostSet(@PathVariable Long id){

        boolean b = hospitalSetService.removeById(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


//    3.条件查询带分页接口
    @ApiOperation(value = "分页条件查询医院信息")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo  hospitalSetQueryVo){
        //创建page类，传递当前页记录数
        Page<HospitalSet> hospitalSetPage=new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        //调用分页查询
        Page<HospitalSet> Page = hospitalSetService.page(hospitalSetPage, wrapper);
        return Result.ok(Page);
    }


//    4.添加医院设置
    @ApiOperation(value = "添加医院信息")
    @PostMapping("saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1可以使用  0不可以使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean b = hospitalSetService.save(hospitalSet);
        return b?Result.ok():Result.fail();
    }


//    5.根据id获取医院设置
    @ApiOperation(value = "根据id获取医院信息")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }
//    6.修改医院设置
    @ApiOperation(value = "修改医院信息")
    @PostMapping("updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b?Result.ok():Result.fail();
    }

    //    7.批量删处医院
    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删处医院信息")
    public Result batchRemoveHospSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    // 8.医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定与解锁")
    @PutMapping("lockHospSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //先根据id查询医院设置的状态值
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置一个状态值
        hospitalSet.setStatus(status);
        //更新数据库信息
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9.发送签名密钥
    @ApiOperation(value = "发送医院签名密钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }

}
